package com.run;

import com.model.Records;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;

public class RecordRun {
    public ResultSet searchRecords(Connection con,Records records) throws SQLException {
        ResultSet resultSet = null;
        String book_id = records.getBook_id();
        if(Objects.equals(records.getBook_id(),""))
        {
            book_id = "null";
        }
        String student_id = records.getStudent_id();
        if(Objects.equals(records.getStudent_id(),""))
        {
            student_id = "null";
        }
        String outdate = records.getOutdate();
        if(Objects.equals(records.getOutdate(),""))
        {
            outdate = "null";
        }
        String backdate = records.getBackdate();
        if(Objects.equals(records.getBackdate(),""))
        {
            backdate = "null";
        }
        if(book_id != "null") {
            String sql = "select * from records where book_id = ?";

            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1,book_id);
            resultSet = pstmt.executeQuery();

//            while(resultSet.next()) {
//                if(Objects.equals(resultSet.getString("book_id"),book_id)) {
//                    resultRecords.setBook_id(resultSet.getString("book_id"));
//                    resultRecords.setStudent_id(resultSet.getString("student_id"));
//                    resultRecords.setOutdate(resultSet.getString("outDate"));
//                    resultRecords.setBackdate(resultSet.getString("backDate"));
//                    break;
//                }
//            }
        }
        else if(student_id != "null") {
            String sql = "select * from records where student_id = ?";

            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1,student_id);
            resultSet = pstmt.executeQuery();

//            while(resultSet.next()) {
//                if(Objects.equals(resultSet.getString("student_id"),student_id)) {
//                    resultRecords.setBook_id(resultSet.getString("book_id"));
//                    resultRecords.setStudent_id(resultSet.getString("student_id"));
//                    resultRecords.setOutdate(resultSet.getString("outDate"));
//                    resultRecords.setBackdate(resultSet.getString("backDate"));
//                    break;
//                }
//            }
        }
        else if(outdate != "null") {
            String sql = "select * from records where outdate = ?";

            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1,outdate);
            resultSet = pstmt.executeQuery();

//            while(resultSet.next()) {
//                if(Objects.equals(resultSet.getString("outdate"),outdate)) {
//                    resultRecords.setBook_id(resultSet.getString("book_id"));
//                    resultRecords.setStudent_id(resultSet.getString("student_id"));
//                    resultRecords.setOutdate(resultSet.getString("outDate"));
//                    resultRecords.setBackdate(resultSet.getString("backDate"));
//                    break;
//                }
//            }
        }
        else if(backdate != "null") {
            String sql = "select * from records where backdate = ?";

            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1,backdate);
            resultSet = pstmt.executeQuery();

//            while(resultSet.next()) {
//                if(Objects.equals(resultSet.getString("backdate"),backdate)) {
//                    resultRecords.setBook_id(resultSet.getString("book_id"));
//                    resultRecords.setStudent_id(resultSet.getString("student_id"));
//                    resultRecords.setOutdate(resultSet.getString("outDate"));
//                    resultRecords.setBackdate(resultSet.getString("backDate"));
//                    break;
//                }
//            }
        }

        return resultSet;
    }
}
